package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.PwdBean;
import com.lwj.liwanjia.bean.SendYzmBean;
import com.lwj.liwanjia.impl.PwdView;
import com.lwj.liwanjia.impl.SendYzmView;
import com.lwj.liwanjia.presenter.SendYzmPresenter;
import com.lwj.liwanjia.presenter.SetPwdPresenter;
import com.lwj.liwanjia.utils.Code;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.TimeCount;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/14.
 */

public class SjYzActivity extends BaseActivity implements SendYzmView, PwdView {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.edit_txm)
    EditText editTxm;
    @BindView(R.id.edit_yzm)
    EditText editYzm;
    @BindView(R.id.iv_txm)
    ImageView ivTxm;
    @BindView(R.id.edit_phone)
    EditText editPhone;

    private String code;
    private String token;
    private SendYzmPresenter yzmPresenter;
    private SetPwdPresenter setPwdPresenter;
    private String code1;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("设置支付密码");
        ivTxm.setImageBitmap(Code.getInstance().createBitmap());
        code = Code.getInstance().getCode();
        code1 = code.toLowerCase();
        token = SpfUtils.getSpfUtils(this).getToken();
        yzmPresenter = new SendYzmPresenter(this);
        setPwdPresenter = new SetPwdPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_change;
    }

    @OnClick({R.id.tv_left, R.id.tv_next, R.id.tv_phone, R.id.iv_txm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.iv_txm:
                ivTxm.setImageBitmap(Code.getInstance().createBitmap());
                code = Code.getInstance().getCode();
                code1 = code.toLowerCase();
                break;
            case R.id.tv_phone:
                String txm = editTxm.getText().toString().trim();
                String phone = editPhone.getText().toString().trim();
                if (TextUtils.isEmpty(txm)) {
                    Toast.makeText(this, "图形码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!code.equals(txm) && !code1.equals(txm)) {
                    Toast.makeText(this, "图形码输入有误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!phone.matches("[1][34578]\\d{9}")) {
                    Toast.makeText(this, "手机号码格式不对", Toast.LENGTH_SHORT).show();
                    return;
                }
                yzmPresenter.sendYzm(token, phone);
                new TimeCount(60000, 1000, tvPhone).start();
                break;
            case R.id.tv_next:
                String pwd = editPwd.getText().toString().trim();
                String yzm = editYzm.getText().toString().trim();
                if (!pwd.matches("^\\d{6}$")) {
                    Toast.makeText(this, "密码只能是6位数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(yzm)) {
                    Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                setPwdPresenter.setPwd(token, pwd, yzm, editPhone.getText().toString().trim());
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void sendYzm(SendYzmBean sendYzmBean) {
        if (sendYzmBean.getState() == 0) {
            Toast.makeText(this, "获取验证码失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setPwd(PwdBean pwdBean) {
        if (pwdBean.getState() == 0) {
            Toast.makeText(this, "设置失败", Toast.LENGTH_SHORT).show();
        } else if (pwdBean.getState() == 1) {
            if (pwdBean.isData()) {
                startActivity(new Intent(this, EditCompleteActivity.class).putExtra("title", "设置支付密码"));
                finish();
            }
        } else if (pwdBean.getState() == 5) {
            Toast.makeText(this, "验证码有误", Toast.LENGTH_SHORT).show();
        } else if (pwdBean.getState() == 4) {
            Toast.makeText(this, "手机号与登录账号不一致", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (yzmPresenter != null) {
            yzmPresenter = null;
        }
        if (setPwdPresenter != null) {
            setPwdPresenter = null;
        }
    }
}
